/// API异常基类
class ApiException implements Exception {
  /// 异常消息
  final String message;
  
  /// HTTP状态码
  final int statusCode;
  
  /// 异常代码
  final String? code;
  
  /// 额外数据
  final Map<String, dynamic>? data;
  
  ApiException({
    required this.message,
    this.statusCode = 500,
    this.code,
    this.data,
  });
  
  @override
  String toString() => 'ApiException: $message (状态码: $statusCode, 错误码: $code)';
}

/// 网络异常 - 表示网络连接问题
class NetworkException extends ApiException {
  NetworkException({
    String message = '网络连接失败，请检查网络设置',
    int statusCode = 0,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'NETWORK_ERROR',
  );
}

/// 超时异常 - 表示请求超时
class TimeoutException extends ApiException {
  TimeoutException({
    String message = '请求超时，请稍后再试',
    int statusCode = 408,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'REQUEST_TIMEOUT',
  );
}

/// 未授权异常 - 表示用户未登录或Token无效
class UnauthorizedException extends ApiException {
  UnauthorizedException({
    String message = '未授权，请重新登录',
    int statusCode = 401,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'UNAUTHORIZED',
  );
}

/// 禁止访问异常 - 表示用户无权限访问
class ForbiddenException extends ApiException {
  ForbiddenException({
    String message = '无权访问此资源',
    int statusCode = 403,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'FORBIDDEN',
  );
}

/// 资源未找到异常 - 表示请求的资源不存在
class NotFoundException extends ApiException {
  NotFoundException({
    String message = '请求的资源不存在',
    int statusCode = 404,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'NOT_FOUND',
  );
}

/// 请求错误异常 - 表示请求参数错误
class BadRequestException extends ApiException {
  BadRequestException({
    String message = '请求参数错误',
    int statusCode = 400,
    String? code,
    Map<String, dynamic>? data,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'BAD_REQUEST',
    data: data,
  );
}

/// 服务器错误异常 - 表示服务器内部错误
class ServerException extends ApiException {
  ServerException({
    String message = '服务器内部错误，请稍后再试',
    int statusCode = 500,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'SERVER_ERROR',
  );
}

/// 数据解析异常 - 表示响应数据解析失败
class ParseException extends ApiException {
  ParseException({
    String message = '数据解析错误',
    int statusCode = 500,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'PARSE_ERROR',
  );
}

/// 取消请求异常 - 表示请求被取消
class CancelException extends ApiException {
  CancelException({
    String message = '请求已取消',
    int statusCode = 499,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'REQUEST_CANCELLED',
  );
}

/// 缓存异常 - 表示缓存操作失败
class CacheException extends ApiException {
  CacheException({
    String message = '缓存操作失败',
    int statusCode = 500,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'CACHE_ERROR',
  );
}

/// 未定义异常 - 表示未明确定义的异常
class UndefinedException extends ApiException {
  UndefinedException({
    String message = '发生未知错误',
    int statusCode = 500,
    String? code,
  }) : super(
    message: message,
    statusCode: statusCode,
    code: code ?? 'UNDEFINED_ERROR',
  );
}

/// 格式异常
class FormatException extends AppException {
  /// 构造函数
  const FormatException(String message, [String? code]) 
    : super(message, code);

  @override
  String toString() => 'FormatException(message: $message, code: $code)';
}

/// 值异常
class ValueException extends AppException {
  /// 构造函数
  const ValueException(String message, [String? code]) 
    : super(message, code);

  @override
  String toString() => 'ValueException(message: $message, code: $code)';
}

/// 数据库异常
class DatabaseException extends AppException {
  /// 构造函数
  const DatabaseException(String message, [String? code]) 
    : super(message, code);

  @override
  String toString() => 'DatabaseException(message: $message, code: $code)';
}

/// 认证异常
class AuthException extends AppException {
  /// 构造函数
  const AuthException(String message, [String? code]) 
    : super(message, code);

  @override
  String toString() => 'AuthException(message: $message, code: $code)';
}

/// 权限异常
class PermissionException extends AppException {
  /// 构造函数
  const PermissionException(String message, [String? code]) 
    : super(message, code);

  @override
  String toString() => 'PermissionException(message: $message, code: $code)';
}

/// 业务逻辑异常
class BusinessException extends AppException {
  BusinessException(String message, {String? code, dynamic details})
      : super(message, code: code, details: details);
}

/// 输入验证异常
class ValidationException extends AppException {
  ValidationException(String message, {String? code, dynamic details})
      : super(message, code: code, details: details);
} 